package com.lkd.controller;

import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.UserService;
import com.lkd.vo.RoleVo;
import com.lkd.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user-service")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页查询人员
     * @param pageIndex
     * @param pageSize
     * @param userName
     * @return
     */
    @GetMapping("/user/search")
    public Result<PageBean<UserVo>> findByPage(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "userName",required = false) String userName){
        PageBean<UserVo> pageBean = userService.findByPage(pageIndex,pageSize,userName);
        return Result.success(pageBean);
    }

    /**
     * 查询角色列表
     * @return
     */
    @GetMapping("/role")
    public Result<List<RoleVo>> findAll(){
        List<RoleVo> voList = userService.findAll();
        return Result.success(voList);
    }

    /**
     * 新增人员
     * @param userVo
     * @return
     */
    @PostMapping("/user")
    public Result save(@RequestBody UserVo userVo){
        userService.save(userVo);
        return Result.success();
    }

    /**
     * 修改人员
     * @param userVo
     * @return
     */
    @PutMapping("/user")
    public Result update(@RequestBody UserVo userVo){
        userService.update(userVo);
        return Result.success();
    }

    /**
     * 删除人员
     * @param userId
     * @return
     */
    @DeleteMapping("/user/{userId}")
    public Result delete(@PathVariable(name = "userId") Integer userId){
        userService.delete(userId);
        return Result.success();
    }
}
